package pcvc.server;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import org.osoa.sca.annotations.Reference;

import pcvc.common.classes.Question;
import pcvc.common.classes.StudentAnswer;
import pcvc.common.services.IPCVCObserverService;
import pcvc.common.services.IPCVCServerStudentService;
import pcvc.common.services.IPCVCServerTeacherService;
import pcvc.common.services.IPCVCSubject;

/**
 * @author SEBASTIAN
 * @version 1.0
 * @created 26-nov-2013 06:43:14 p.m.
 */
public class PCVCServer implements IPCVCServerTeacherService, IPCVCServerStudentService, IPCVCSubject
{
    @Reference(name="observerService1")
    private IPCVCObserverService pcvcStudent1;
    @Reference(name="observerService2")
    private IPCVCObserverService pcvcStudent2;
    @Reference(name="observerService3")
    private IPCVCObserverService pcvcStudent3;
    @Reference(name="observerService4")
    private IPCVCObserverService pcvcStudent4;
    @Reference(name="observerTeacher")
    private IPCVCObserverService pcvcTeacher;
    
    private List<String> comments;
    public List<IPCVCObserverService> observers;
    
    public Question currentQuestion;
    public List<Question> finishedQuestions;

    public PCVCServer( )
    {
        currentQuestion = null;
        comments = new ArrayList<String>( );
        observers = new ArrayList<IPCVCObserverService>( );
        finishedQuestions = new ArrayList<Question>( );
    }

    /**
     * Adds a comments to the list of comments
     * @param comment The comment to be added
     */
    @Override
    public void addComment( String comment )
    {
        comments.add( comment );
        notifyAll( );
    }

    /**
     * Adds a student answer to the current question
     * @param studentAnswer
     */
    @Override
    public void addStudentAnswer( StudentAnswer studentAnswer )
    {
        if( currentQuestion != null )
        {
            currentQuestion.getCorrectAnswer( );
        }

        notifyAll( );
    }

    /**
     * Returns the answers of the current question given by the students
     */
    @Override
    public Set<StudentAnswer> getAnswers( )
    {
        Set<StudentAnswer> resp = null;

        if( currentQuestion != null )
        {
            resp = currentQuestion.getStudentsAnswers( );
        }

        return resp;
    }

    /**
     * 
     */
    @Override
    public String getCurrentQuestion( )
    {
        String resp = "There isn't a question to be answered yet.";

        if( currentQuestion != null )
        {
            resp = "Q" + currentQuestion.getNumber( ) + ": " + currentQuestion.getQuestion( ) + "\n Equation: " + currentQuestion.getEquation( );
        }

        return resp;
    }

    public void sendQuestion( Question question )
    {
        finishedQuestions.add( currentQuestion );
        currentQuestion = question;
        notifyAll( );
    }

    @Override
    public List<String> getComments( )
    {
        return comments;
    }
    
    public StudentAnswer getAnswer( int studentId )
    {
        StudentAnswer resp = null;
        
        if( currentQuestion != null )
        {
            Iterator<StudentAnswer> iterator = currentQuestion.getStudentsAnswers( ).iterator( );
            
            while(iterator.hasNext( ))
            {
                if(iterator.next( ).getStudentId( ) == studentId)
                {
                    resp = iterator.next( );
                }
            }
        }
        
        return resp;
    }

    @Override
    public void attach( IPCVCObserverService observer )
    {
        observers.add( observer );
    }

    @Override
    public void detach( IPCVCObserverService observer )
    {
        observers.remove( observer );
    }

    @Override
    public void notifyObservers( )
    {
        Iterator<IPCVCObserverService> iterador = observers.iterator( );

        while( iterador.hasNext( ) )
        {
            iterador.next( ).update( );
        }
    }
}